package kube_api

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"net/http"
	"virtual-resource-schedule-service/api"
	"virtual-resource-schedule-service/middleware"
	pod2 "virtual-resource-schedule-service/model/kube_pod"
	"virtual-resource-schedule-service/service/kube_service"
)

func CreateContainer(ctx *gin.Context) {
	podRequest := pod2.PodRequest{}
	if err := ctx.ShouldBindJSON(&podRequest); err != nil {
		api.HandleValidatorError(ctx, err)
	}
	zap.S().Infof("Create kube_pod request: %+v", podRequest)
	service := kube_service.NewPodApiService(podRequest.PodClusterId)
	response, err := service.CreateContainer(podRequest)
	if err != nil {
		zap.S().Error("创建容器失败!")
		resp := middleware.APIResponse{
			Code:    "10000503",
			Message: err.Error(),
			Data:    nil,
			Success: false,
		}
		middleware.ResponseJSON(ctx, http.StatusInternalServerError, resp)
		return
	}
	resp := middleware.APIResponse{
		Code:    "00000000",
		Message: "容器创建成功",
		Data:    response,
		Success: true,
	}
	middleware.ResponseJSON(ctx, http.StatusOK, resp)

}

func StartContainer(ctx *gin.Context) {
	startRequest := pod2.StartPodReq{}
	if err := ctx.ShouldBindJSON(&startRequest); err != nil {
		api.HandleValidatorError(ctx, err)
	}
	zap.S().Infof("Start kube_pod request: %+v", startRequest)
	service := kube_service.NewPodApiService(startRequest.PodClusterId)
	response, err := service.StartContainer(startRequest)
	if err != nil {
		zap.S().Error("容器开机失败!")
		resp := middleware.APIResponse{
			Code:    "10000503",
			Message: "容器开机失败",
			Data:    nil,
			Success: false,
		}
		middleware.ResponseJSON(ctx, http.StatusInternalServerError, resp)
		return
	}
	resp := middleware.APIResponse{
		Code:    "00000000",
		Message: "容器开机成功",
		Data:    response,
		Success: true,
	}
	middleware.ResponseJSON(ctx, http.StatusOK, resp)
}

func ShutdownContainer(ctx *gin.Context) {
	shutdownPodReq := pod2.ShutdownPodReq{}
	if err := ctx.ShouldBindJSON(&shutdownPodReq); err != nil {
		api.HandleValidatorError(ctx, err)
	}
	zap.S().Infof("Shutdown kube_pod request: %+v", shutdownPodReq)
	service := kube_service.NewPodApiService(shutdownPodReq.PodClusterId)
	response, err := service.ShutdownContainer(shutdownPodReq)
	if err != nil {
		zap.S().Error("容器关机失败!")
		resp := middleware.APIResponse{
			Code:    "10000503",
			Message: "容器关机失败",
			Data:    nil,
			Success: false,
		}
		middleware.ResponseJSON(ctx, http.StatusInternalServerError, resp)
		return
	}
	resp := middleware.APIResponse{
		Code:    "00000000",
		Message: "容器关机成功",
		Data:    response,
		Success: true,
	}
	middleware.ResponseJSON(ctx, http.StatusOK, resp)
}
